package cn.lbz.Food.Edit;

import cn.lbz.Food.Init.DBUtils;
import cn.lbz.Food.Init.DBfood;
import cn.lbz.Food.Init.Page;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serial;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

public class UpdateFoodGet extends HttpServlet{

    @Serial
    private static final long serialVersionUID = 8421891245981180040L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");

        String sql = "SELECT * FROM share_food WHERE id=" + id;
        try {
            List<DBfood> upfood = DBUtils.getfood(sql);
            if (upfood.size() == 0) {
                response.sendRedirect("./editfood");
                return;
            }

            DBfood bfood = upfood.get(0);

            StringBuilder sb = new StringBuilder();
            sb.append(Page.getHeader());
            sb.append(buildFoodForm(bfood));
            sb.append(Page.getEND());

            response.setCharacterEncoding("UTF-8");
            try (Writer writer = response.getWriter()) {
                writer.write(sb.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String buildFoodForm(DBfood bfood) {
        String html = String.format("""
                        <section id="addfood" class="addfood">
                                <div class="container" data-aos="fade-up">
                                    <div class="section-title">
                                        <p>更新美食！</p>
                                    </div>
                                    <form action="./updatefood" method="post" enctype="multipart/form-data" class="form-basic" data-aos="fade-up" data-aos-delay="100">
                                        <div class="row">
                                            <div class="col-lg-4 col-md-6 form-group">
                                                <input type="text" name="name" class="form-control" id="name" placeholder="食物名称">
                                                <div class="validate"></div>
                                            </div>
                                            <div class="col-lg-4 col-md-6 form-group mt-3 mt-md-0">
                                                <input type="text" class="form-control" name="located" id="located" placeholder="地点">
                                                <div class="validate"></div>
                                            </div>
                                            <div class="col-lg-4 col-md-6 form-group mt-3 mt-md-0">
                                                <input type="text" class="form-control" name="price" id="price" placeholder="价格">
                                                <div class="validate"></div>
                                            </div>
                                            <div class="col-lg-4 col-md-6 form-group mt-3">
                                                <select name="type" class="form-control" id="type" style="color: #a49b89;">
                                                    <option value='mian' style="color: white;">面食
                                                    <option value='rice' style="color: white;">米饭
                                                    <option value='bbq' style="color: white;">烧烤
                                                    <option value='zizhu' style="color: white;">自助
                                                    <option value="other" style="color: white;">其他
                                                </select>
                                                <div class="validate"></div>
                                            </div>
                                          </div>
                                        <div class="form-group mt-3">
                                            <textarea class="form-control" name="reason" rows="5" placeholder="推荐原因"></textarea>
                                            <div class="validate"></div>
                                        </div>
                                        <div class="text-center"><button type="submit">更新</button></div>
                                    </form>
                                </div>
                            </section>""",bfood.getId(), bfood.getName(), bfood.getLocated(), bfood.getPrice(), bfood.getreason()
        );

        html = html.replace("'" + bfood.getType() + "'", "'" + bfood.getType() + "' selected" );
        return html;
    }
}
